package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.data.ItemClass;
import com.lineage.data.item_armor.set.ArmorSet;
import com.lineage.server.IdFactory;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.templates.L1Armor;
import com.lineage.server.templates.L1EtcItem;
import com.lineage.server.templates.L1Item;
import com.lineage.server.templates.L1Weapon;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ItemTable {
  private static final Log _log = LogFactory.getLog(ItemTable.class);
  
  private static final Map<String, Integer> _armorTypes = new HashMap<>();
  
  private static final Map<String, Integer> _weaponTypes = new HashMap<>();
  
  private static final Map<String, Integer> _weaponId = new HashMap<>();
  
  private static final Map<String, Integer> _materialTypes = new HashMap<>();
  
  private static final Map<String, Integer> _etcItemTypes = new HashMap<>();
  
  private static final Map<String, Integer> _useTypes = new HashMap<>();
  
  private static ItemTable _instance;
  
  private static Map<Integer, L1EtcItem> _etcitems;
  
  private static Map<Integer, L1Armor> _armors;
  
  private static Map<Integer, L1Weapon> _weapons;
  
  private L1Item[] _allTemplates;
  
  static {
    _etcItemTypes.put("arrow", new Integer(0));
    _etcItemTypes.put("wand", new Integer(1));
    _etcItemTypes.put("light", new Integer(2));
    _etcItemTypes.put("gem", new Integer(3));
    _etcItemTypes.put("totem", new Integer(4));
    _etcItemTypes.put("firecracker", new Integer(5));
    _etcItemTypes.put("potion", new Integer(6));
    _etcItemTypes.put("food", new Integer(7));
    _etcItemTypes.put("scroll", new Integer(8));
    _etcItemTypes.put("questitem", new Integer(9));
    _etcItemTypes.put("spellbook", new Integer(10));
    _etcItemTypes.put("petitem", new Integer(11));
    _etcItemTypes.put("other", new Integer(12));
    _etcItemTypes.put("material", new Integer(13));
    _etcItemTypes.put("event", new Integer(14));
    _etcItemTypes.put("sting", new Integer(15));
    _etcItemTypes.put("treasure_box", new Integer(16));
    _etcItemTypes.put("ItemIntegration", new Integer(19));
    _etcItemTypes.put("scrollshop", new Integer(20));
    _useTypes.put("petitem", new Integer(-12));
    _useTypes.put("other", new Integer(-11));
    _useTypes.put("power", new Integer(-10));
    _useTypes.put("book", new Integer(-9));
    _useTypes.put("makecooking", new Integer(-8));
    _useTypes.put("hpr", new Integer(-7));
    _useTypes.put("mpr", new Integer(-6));
    _useTypes.put("ticket", new Integer(-5));
    _useTypes.put("petcollar", new Integer(-4));
    _useTypes.put("sting", new Integer(-3));
    _useTypes.put("arrow", new Integer(-2));
    _useTypes.put("none", new Integer(-1));
    _useTypes.put("normal", new Integer(0));
    _useTypes.put("weapon", new Integer(1));
    _useTypes.put("armor", new Integer(2));
    _useTypes.put("spell_1", new Integer(3));
    _useTypes.put("4", new Integer(4));
    _useTypes.put("spell_long", new Integer(5));
    _useTypes.put("ntele", new Integer(6));
    _useTypes.put("identify", new Integer(7));
    _useTypes.put("res", new Integer(8));
    _useTypes.put("home", new Integer(9));
    _useTypes.put("light", new Integer(10));
    _useTypes.put("letter", new Integer(12));
    _useTypes.put("letter_card", new Integer(13));
    _useTypes.put("choice", new Integer(14));
    _useTypes.put("instrument", new Integer(15));
    _useTypes.put("sosc", new Integer(16));
    _useTypes.put("spell_short", new Integer(17));
    _useTypes.put("T", new Integer(18));
    _useTypes.put("cloak", new Integer(19));
    _useTypes.put("glove", new Integer(20));
    _useTypes.put("boots", new Integer(21));
    _useTypes.put("helm", new Integer(22));
    _useTypes.put("ring", new Integer(23));
    _useTypes.put("amulet", new Integer(24));
    _useTypes.put("shield", new Integer(25));
    _useTypes.put("guarder", new Integer(25));
    _useTypes.put("dai", new Integer(26));
    _useTypes.put("zel", new Integer(27));
    _useTypes.put("blank", new Integer(28));
    _useTypes.put("btele", new Integer(29));
    _useTypes.put("spell_buff", new Integer(30));
    _useTypes.put("ccard", new Integer(31));
    _useTypes.put("ccard_w", new Integer(32));
    _useTypes.put("vcard", new Integer(33));
    _useTypes.put("vcard_w", new Integer(34));
    _useTypes.put("wcard", new Integer(35));
    _useTypes.put("wcard_w", new Integer(36));
    _useTypes.put("belt", new Integer(37));
    _useTypes.put("food", new Integer(38));
    _useTypes.put("spell_long2", new Integer(39));
    _useTypes.put("earring", new Integer(40));
    _useTypes.put("fishing_rod", new Integer(42));
    _useTypes.put("runeword_left", new Integer(43));
    _useTypes.put("runeword_right", new Integer(44));
    _useTypes.put("runeword_middle", new Integer(45));
    _useTypes.put("enc", new Integer(46));
    _useTypes.put("pants", new Integer(47));
    _useTypes.put("talisman", new Integer(48));
    _useTypes.put("talisman2", new Integer(49));
    _useTypes.put("talisman3", new Integer(51));
    _useTypes.put("choice_doll", new Integer(55));
    _armorTypes.put("none", new Integer(0));
    _armorTypes.put("helm", new Integer(1));
    _armorTypes.put("armor", new Integer(2));
    _armorTypes.put("T", new Integer(3));
    _armorTypes.put("cloak", new Integer(4));
    _armorTypes.put("glove", new Integer(5));
    _armorTypes.put("boots", new Integer(6));
    _armorTypes.put("shield", new Integer(7));
    _armorTypes.put("amulet", new Integer(8));
    _armorTypes.put("ring", new Integer(9));
    _armorTypes.put("belt", new Integer(10));
    _armorTypes.put("ring2", new Integer(11));
    _armorTypes.put("earring", new Integer(12));
    _armorTypes.put("guarder", new Integer(13));
    _armorTypes.put("runeword_left", new Integer(14));
    _armorTypes.put("runeword_right", new Integer(15));
    _armorTypes.put("runeword_middle", new Integer(16));
    _armorTypes.put("pants", new Integer(17));
    _armorTypes.put("talisman", new Integer(18));
    _armorTypes.put("talisman2", new Integer(19));
    _armorTypes.put("talisman3", new Integer(20));
    _weaponTypes.put("none", new Integer(0));
    _weaponTypes.put("sword", new Integer(1));
    _weaponTypes.put("dagger", new Integer(2));
    _weaponTypes.put("tohandsword", new Integer(3));
    _weaponTypes.put("bow", new Integer(4));
    _weaponTypes.put("spear", new Integer(5));
    _weaponTypes.put("blunt", new Integer(6));
    _weaponTypes.put("staff", new Integer(7));
    _weaponTypes.put("throwingknife", new Integer(8));
    _weaponTypes.put("arrow", new Integer(9));
    _weaponTypes.put("gauntlet", new Integer(10));
    _weaponTypes.put("claw", new Integer(11));
    _weaponTypes.put("edoryu", new Integer(12));
    _weaponTypes.put("singlebow", new Integer(13));
    _weaponTypes.put("singlespear", new Integer(14));
    _weaponTypes.put("tohandblunt", new Integer(15));
    _weaponTypes.put("tohandstaff", new Integer(16));
    _weaponTypes.put("kiringku", new Integer(17));
    _weaponTypes.put("chainsword", new Integer(18));
    _weaponId.put("sword", new Integer(4));
    _weaponId.put("dagger", new Integer(46));
    _weaponId.put("tohandsword", new Integer(50));
    _weaponId.put("bow", new Integer(20));
    _weaponId.put("blunt", new Integer(11));
    _weaponId.put("spear", new Integer(24));
    _weaponId.put("staff", new Integer(40));
    _weaponId.put("throwingknife", new Integer(2922));
    _weaponId.put("arrow", new Integer(66));
    _weaponId.put("gauntlet", new Integer(62));
    _weaponId.put("claw", new Integer(58));
    _weaponId.put("edoryu", new Integer(54));
    _weaponId.put("singlebow", new Integer(20));
    _weaponId.put("singlespear", new Integer(24));
    _weaponId.put("tohandblunt", new Integer(11));
    _weaponId.put("tohandstaff", new Integer(40));
    _weaponId.put("kiringku", new Integer(58));
    _weaponId.put("chainsword", new Integer(24));
    _materialTypes.put("none", new Integer(0));
    _materialTypes.put("liquid", new Integer(1));
    _materialTypes.put("web", new Integer(2));
    _materialTypes.put("vegetation", new Integer(3));
    _materialTypes.put("animalmatter", new Integer(4));
    _materialTypes.put("paper", new Integer(5));
    _materialTypes.put("cloth", new Integer(6));
    _materialTypes.put("leather", new Integer(7));
    _materialTypes.put("wood", new Integer(8));
    _materialTypes.put("bone", new Integer(9));
    _materialTypes.put("dragonscale", new Integer(10));
    _materialTypes.put("iron", new Integer(11));
    _materialTypes.put("steel", new Integer(12));
    _materialTypes.put("copper", new Integer(13));
    _materialTypes.put("silver", new Integer(14));
    _materialTypes.put("gold", new Integer(15));
    _materialTypes.put("platinum", new Integer(16));
    _materialTypes.put("mithril", new Integer(17));
    _materialTypes.put("blackmithril", new Integer(18));
    _materialTypes.put("glass", new Integer(19));
    _materialTypes.put("gemstone", new Integer(20));
    _materialTypes.put("mineral", new Integer(21));
    _materialTypes.put("oriharukon", new Integer(22));
  }
  
  public static ItemTable get() {
    if (_instance == null)
      _instance = new ItemTable(); 
    return _instance;
  }
  
  public static void init() {
    _instance = new ItemTable();
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    _etcitems = allEtcItem();
    _weapons = allWeapon();
    _armors = allArmor();
    buildFastLookupTable();
    _log.info("载入道具,武器,防具资料: " + _etcitems.size() + "+" + _weapons.size() + "+" + 
        _armors.size() + "=" + (
        _etcitems.size() + _weapons.size() + _armors.size()) + "(" + 
        timer.get() + "ms)");
  }
  
  public void loadarmors() {
    PerformanceTimer timer = new PerformanceTimer();
    _armors = allArmor();
    buildFastLookupTable();
    _log.info("载入防具资料: " + _armors.size() + "=" + "(" + timer.get() + "ms)");
  }
  
  public void loadweapons() {
    PerformanceTimer timer = new PerformanceTimer();
    _weapons = allWeapon();
    buildFastLookupTable();
    _log.info("载入武器资料: " + _weapons.size() + "=" + "(" + timer.get() + "ms)");
  }
  
  public void loaditems() {
    PerformanceTimer timer = new PerformanceTimer();
    _etcitems = allEtcItem();
    buildFastLookupTable();
    _log.info("载入道具资料: " + _etcitems.size() + "=" + "(" + timer.get() + "ms)");
  }
  
  private Map<Integer, L1EtcItem> allEtcItem() {
    Map<Integer, L1EtcItem> result = new HashMap<>();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    L1EtcItem item = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `etcitem`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        item = new L1EtcItem();
        int itemid = rs.getInt("item_id");
        item.setItemId(itemid);
        item.setName(rs.getString("name"));
        String classname = rs.getString("classname");
        item.setClassname(classname);
        item.setNameId(rs.getString("name_id"));
        item.setType(((Integer)_etcItemTypes.get(rs.getString("item_type"))).intValue());
        item.setUseType(((Integer)_useTypes.get(rs.getString("use_type"))).intValue());
        item.setType2(0);
        item.setMaterial(((Integer)_materialTypes.get(rs.getString("material"))).intValue());
        item.setWeight(rs.getInt("weight"));
        item.setGfxId(rs.getInt("invgfx"));
        item.setGroundGfxId(rs.getInt("grdgfx"));
        item.setItemDescId(rs.getInt("itemdesc_id"));
        item.setMinLevel(rs.getInt("最小等级"));
        item.setMaxLevel(rs.getInt("最大等级"));
        item.setBless(rs.getInt("是否祝福"));
        item.setTradable((rs.getInt("不能交易") == 0));
        item.setCantDelete((rs.getInt("不能删除") == 1));
        item.setDmgSmall(rs.getInt("最小伤害"));
        item.setDmgLarge(rs.getInt("最大伤害"));
        item.set_stackable((rs.getInt("是否重叠") == 1));
        item.setMaxChargeCount(rs.getInt("使用次数"));
        item.setMaxUseTime(rs.getInt("使用期限"));
        item.set_delayid(rs.getInt("delay_id"));
        item.set_delaytime(rs.getInt("delay_time"));
        item.set_delayEffect(rs.getInt("delay_effect"));
        item.setFoodVolume(rs.getInt("饱食度"));
        item.setToBeSavedAtOnce((rs.getInt("save_at_once") == 1));
        item.setminlogpcpower(rs.getInt("最小转生次数"));
        item.setmaxlogpcpower(rs.getInt("最大转生次数"));
        item.setacccount(rs.getInt("帐号使用一次"));
        item.setcharcount(rs.getInt("角色使用一次"));
        item.setnoprivateshop(rs.getInt("不能摆摊"));
        item.setnodrop((rs.getInt("是否喷装") != 0));
        ItemClass.get().addList(itemid, classname, 0);
        result.put(new Integer(item.getItemId()), item);
      } 
    } catch (NullPointerException e) {
      _log.error("加载失败: " + item.getItemId(), e);
    } catch (SQLException e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    return result;
  }
  
  private Map<Integer, L1Weapon> allWeapon() {
    Map<Integer, L1Weapon> result = new HashMap<>();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    L1Weapon weapon = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `weapon`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        weapon = new L1Weapon();
        int itemid = rs.getInt("item_id");
        weapon.setItemId(itemid);
        weapon.setName(rs.getString("name"));
        String classname = rs.getString("classname");
        weapon.setClassname(classname);
        weapon.setNameId(rs.getString("name_id"));
        weapon.setType(((Integer)_weaponTypes.get(rs.getString("type"))).intValue());
        weapon.setType1(((Integer)_weaponId.get(rs.getString("type"))).intValue());
        weapon.setType2(1);
        weapon.setUseType(1);
        weapon.setMaterial(((Integer)_materialTypes.get(rs.getString("material"))).intValue());
        weapon.setWeight(rs.getInt("weight"));
        weapon.setGfxId(rs.getInt("invgfx"));
        weapon.setGroundGfxId(rs.getInt("grdgfx"));
        weapon.setItemDescId(rs.getInt("itemdesc_id"));
        weapon.setDmgSmall(rs.getInt("最小伤害"));
        weapon.setDmgLarge(rs.getInt("最大伤害"));
        weapon.setRange(rs.getInt("攻击距离"));
        weapon.set_safeenchant(rs.getInt("安定值"));
        weapon.setUseRoyal((rs.getInt("王族") != 0));
        weapon.setUseKnight((rs.getInt("骑士") != 0));
        weapon.setUseElf((rs.getInt("妖精") != 0));
        weapon.setUseMage((rs.getInt("法师") != 0));
        weapon.setUseDarkelf((rs.getInt("黑妖") != 0));
        weapon.setUseDragonknight((rs.getInt("龙骑") != 0));
        weapon.setUseIllusionist((rs.getInt("幻术") != 0));
        weapon.setHitModifier(rs.getInt("命中"));
        weapon.setDmgModifier(rs.getInt("额外伤害"));
        weapon.set_addstr(rs.getByte("力量"));
        weapon.set_adddex(rs.getByte("敏捷"));
        weapon.set_addcon(rs.getByte("体质"));
        weapon.set_addint(rs.getByte("智力"));
        weapon.set_addwis(rs.getByte("精神"));
        weapon.set_addcha(rs.getByte("魅力"));
        weapon.set_addhp(rs.getInt("血量"));
        weapon.set_addmp(rs.getInt("魔量"));
        weapon.set_addhpr(rs.getInt("回血"));
        weapon.set_addmpr(rs.getInt("回魔"));
        weapon.set_addsp(rs.getInt("魔攻"));
        weapon.set_mdef(rs.getInt("魔防"));
        weapon.setDoubleDmgChance(rs.getInt("双刀爪暴击率"));
        weapon.setMagicDmgModifier(rs.getInt("魔法伤害"));
        weapon.set_canbedmg(rs.getInt("不会坏刀"));
        weapon.setMinLevel(rs.getInt("最小等级"));
        weapon.setMaxLevel(rs.getInt("最大等级"));
        weapon.setBless(rs.getInt("是否祝福"));
        weapon.setTradable((rs.getInt("不能交易") == 0));
        weapon.setCantDelete((rs.getInt("不能删除") == 1));
        weapon.setHasteItem((rs.getInt("自动加速") != 0));
        weapon.setMaxUseTime(rs.getInt("使用期限"));
        weapon.setExpPoint(rs.getInt("经验百分比"));
        weapon.setPVPdmg(rs.getInt("PVP额外伤害"));
        weapon.setPVPdmgReduction(rs.getInt("PVP伤害减免"));
        weapon.setpenetrate(rs.getInt("贯穿效果"));
        weapon.setNoweaponRedmg(rs.getInt("无视伤害减免"));
        weapon.setaddStunLevel(rs.getInt("昏迷命中率"));
        weapon.setdropcolor((rs.getInt("装备炫彩") != 0));
        weapon.setgivebless((rs.getInt("是否可祝福") != 0));
        weapon.setnodrop((rs.getInt("是否喷装") != 0));
        ItemClass.get().addList(itemid, classname, 1);
        result.put(new Integer(weapon.getItemId()), weapon);
      } 
    } catch (NullPointerException e) {
      _log.error("加载失败: " + weapon.getItemId(), e);
    } catch (SQLException e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    return result;
  }
  
  private Map<Integer, L1Armor> allArmor() {
    Map<Integer, L1Armor> result = new HashMap<>();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    L1Armor armor = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `armor`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        armor = new L1Armor();
        int itemid = rs.getInt("item_id");
        armor.setItemId(itemid);
        armor.setName(rs.getString("name"));
        String classname = rs.getString("classname");
        armor.setClassname(classname);
        armor.setNameId(rs.getString("name_id"));
        armor.setType(((Integer)_armorTypes.get(rs.getString("type"))).intValue());
        armor.setType2(2);
        armor.setUseType(((Integer)_useTypes.get(rs.getString("type"))).intValue());
        armor.setMaterial(((Integer)_materialTypes.get(rs.getString("material"))).intValue());
        armor.setWeight(rs.getInt("weight"));
        armor.setGfxId(rs.getInt("invgfx"));
        armor.setGroundGfxId(rs.getInt("grdgfx"));
        armor.setItemDescId(rs.getInt("itemdesc_id"));
        armor.set_ac(rs.getInt("防御"));
        armor.set_safeenchant(rs.getInt("安定值"));
        armor.setUseRoyal((rs.getInt("王族") != 0));
        armor.setUseKnight((rs.getInt("骑士") != 0));
        armor.setUseElf((rs.getInt("妖精") != 0));
        armor.setUseMage((rs.getInt("法师") != 0));
        armor.setUseDarkelf((rs.getInt("黑妖") != 0));
        armor.setUseDragonknight((rs.getInt("龙骑") != 0));
        armor.setUseIllusionist((rs.getInt("幻术") != 0));
        armor.set_addstr(rs.getByte("力量"));
        armor.set_addcon(rs.getByte("体质"));
        armor.set_adddex(rs.getByte("敏捷"));
        armor.set_addint(rs.getByte("智力"));
        armor.set_addwis(rs.getByte("精神"));
        armor.set_addcha(rs.getByte("魅力"));
        armor.set_addhp(rs.getInt("血量"));
        armor.set_addmp(rs.getInt("魔量"));
        armor.set_addhpr(rs.getInt("回血"));
        armor.set_addmpr(rs.getInt("回魔"));
        armor.set_addsp(rs.getInt("魔攻"));
        armor.setMinLevel(rs.getInt("最小等级"));
        armor.setMaxLevel(rs.getInt("最大等级"));
        armor.set_mdef(rs.getInt("魔防"));
        armor.setDamageReduction(rs.getInt("伤害减免"));
        armor.setWeightReduction(rs.getInt("负重减免"));
        armor.setHitModifierByArmor(rs.getInt("近战命中"));
        armor.setDmgModifierByArmor(rs.getInt("近战伤害"));
        armor.setBowHitModifierByArmor(rs.getInt("远战命中"));
        armor.setBowDmgModifierByArmor(rs.getInt("远战伤害"));
        armor.setHasteItem((rs.getInt("自动加速") != 0));
        armor.setBless(rs.getInt("是否祝福"));
        armor.setTradable((rs.getInt("不能交易") == 0));
        armor.setCantDelete((rs.getInt("不能删除") == 1));
        armor.set_defense_earth(rs.getInt("地属性"));
        armor.set_defense_water(rs.getInt("水属性"));
        armor.set_defense_wind(rs.getInt("风属性"));
        armor.set_defense_fire(rs.getInt("火属性"));
        armor.set_regist_stun(rs.getInt("晕眩耐性"));
        armor.set_regist_stone(rs.getInt("石化耐性"));
        armor.set_regist_sleep(rs.getInt("睡眠耐性"));
        armor.set_regist_freeze(rs.getInt("寒冰耐性"));
        armor.set_regist_sustain(rs.getInt("支撑耐性"));
        armor.set_regist_blind(rs.getInt("暗闇耐性"));
        armor.setMaxUseTime(rs.getInt("使用期限"));
        armor.set_greater(rs.getInt("greater"));
        armor.setExpPoint(rs.getInt("经验百分比"));
        armor.setMagicHitModifierByArmor(rs.getInt("魔法命中"));
        armor.set_up_hp_potion(rs.getInt("药水回复百分比"));
        armor.set_uhp_number(rs.getInt("药水回复量"));
        armor.setPVPdmg(rs.getInt("PVP额外伤害"));
        armor.setPVPdmgReduction(rs.getInt("PVP伤害减免"));
        armor.setdropcolor((rs.getInt("装备炫彩") != 0));
        armor.setgivebless((rs.getInt("是否可祝福") != 0));
        armor.setgroup(rs.getInt("同系列装备"));
        armor.setnodrop((rs.getInt("是否喷装") != 0));
        ItemClass.get().addList(itemid, classname, 2);
        result.put(new Integer(armor.getItemId()), armor);
      } 
    } catch (NullPointerException e) {
      _log.error("加载失败: " + armor.getItemId(), e);
    } catch (SQLException e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    return result;
  }
  
  private void buildFastLookupTable() {
    int highestId = 0;
    Collection<L1EtcItem> items = _etcitems.values();
    Iterator<L1EtcItem> iterator = items.iterator();
    while (iterator.hasNext()) {
      L1EtcItem item = iterator.next();
      if (item.getItemId() > highestId)
        highestId = item.getItemId(); 
    } 
    Collection<L1Weapon> weapons = _weapons.values();
    Iterator<L1Weapon> iterator2 = weapons.iterator();
    while (iterator2.hasNext()) {
      L1Weapon weapon = iterator2.next();
      if (weapon.getItemId() > highestId)
        highestId = weapon.getItemId(); 
    } 
    Collection<L1Armor> armors = _armors.values();
    Iterator<L1Armor> iterator3 = armors.iterator();
    while (iterator3.hasNext()) {
      L1Armor armor = iterator3.next();
      if (armor.getItemId() > highestId)
        highestId = armor.getItemId(); 
    } 
    this._allTemplates = new L1Item[highestId + 1];
    Iterator<Integer> iter = _etcitems.keySet().iterator();
    while (iter.hasNext()) {
      Integer id = iter.next();
      L1EtcItem item2 = _etcitems.get(id);
      this._allTemplates[id.intValue()] = (L1Item)item2;
    } 
    iter = _weapons.keySet().iterator();
    while (iter.hasNext()) {
      Integer id = iter.next();
      L1Weapon item3 = _weapons.get(id);
      this._allTemplates[id.intValue()] = (L1Item)item3;
    } 
    iter = _armors.keySet().iterator();
    while (iter.hasNext()) {
      Integer id = iter.next();
      L1Armor item4 = _armors.get(id);
      this._allTemplates[id.intValue()] = (L1Item)item4;
    } 
  }
  
  public void se_mode() {
    PerformanceTimer timer = new PerformanceTimer();
    L1Item[] allTemplates;
    int length = (allTemplates = this._allTemplates).length;
    int i = 0;
    while (i < length) {
      L1Item item = allTemplates[i];
      if (item != null) {
        Iterator<Integer> iterator = ArmorSet.getAllSet().keySet().iterator();
        while (iterator.hasNext()) {
          Integer key = iterator.next();
          ArmorSet armorSet = (ArmorSet)ArmorSet.getAllSet().get(key);
          if (armorSet.isPartOfSet(item.getItemId()))
            item.set_mode(armorSet.get_mode()); 
        } 
      } 
      i++;
    } 
    _log.info("载入套装效果数字阵列: " + timer.get() + "ms)");
  }
  
  public L1Item getTemplate(int itemid) {
    try {
      return this._allTemplates[itemid];
    } catch (Exception ex) {
      return null;
    } 
  }
  
  public L1Item getTemplate(String nameid) {
    L1Item[] allTemplates;
    int length = (allTemplates = this._allTemplates).length;
    int i = 0;
    while (i < length) {
      L1Item item = allTemplates[i];
      if (item != null && item.getNameId().equals(nameid))
        return item; 
      i++;
    } 
    return null;
  }
  
  public L1ItemInstance createItem(int itemId) {
    L1Item temp = getTemplate(itemId);
    if (temp == null)
      return null; 
    L1ItemInstance item = new L1ItemInstance();
    item.setId(IdFactory.get().nextId());
    item.setItem(temp);
    item.setBless(temp.getBless());
    World.get().storeObject((L1Object)item);
    return item;
  }
  
  public int findItemIdByName(String name) {
    int itemid = 0;
    L1Item[] allTemplates;
    int length = (allTemplates = this._allTemplates).length;
    int i = 0;
    while (i < length) {
      L1Item item = allTemplates[i];
      if (item != null && item.getNameId().equals(name)) {
        itemid = item.getItemId();
        break;
      } 
      i++;
    } 
    return itemid;
  }
  
  public int findItemIdByNameWithoutSpace(String name) {
    int itemid = 0;
    L1Item[] allTemplates;
    int length = (allTemplates = this._allTemplates).length;
    int i = 0;
    while (i < length) {
      L1Item item = allTemplates[i];
      if (item != null && item.getNameId().replace(" ", "").equals(name)) {
        itemid = item.getItemId();
        break;
      } 
      i++;
    } 
    return itemid;
  }
  
  public int findItemIdByNameWithoutSpace1(String name) {
    int itemid = 0;
    L1Item[] allTemplates;
    int length = (allTemplates = this._allTemplates).length;
    int i = 0;
    while (i < length) {
      L1Item item = allTemplates[i];
      if (item != null && item.getName().replace(" ", "").equals(name)) {
        itemid = item.getItemId();
        break;
      } 
      i++;
    } 
    return itemid;
  }
}
